

package com.maoshi.shop.api.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.maoshi.shop.bean.app.dto.ShopHeadInfoDto;
import com.maoshi.shop.bean.dto.ShopDetailParam;
import com.maoshi.shop.bean.model.Product;
import com.maoshi.shop.bean.model.ShopAuditing;
import com.maoshi.shop.bean.model.ShopDetail;
import com.maoshi.shop.bean.model.UserCollectionShop;
import com.maoshi.shop.common.exception.MaoshiShopBindException;
import com.maoshi.shop.common.util.PageParam;
import com.maoshi.shop.security.api.util.SecurityUtils;
import com.maoshi.shop.security.comment.model.UpdatePasswordDto;
import com.maoshi.shop.security.comment.model.UsernameAndPasswordDto;
import com.maoshi.shop.service.ProductService;
import com.maoshi.shop.service.ShopAuditingService;
import com.maoshi.shop.service.ShopDetailService;
import com.maoshi.shop.service.UserCollectionShopService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import ma.glasnost.orika.MapperFacade;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;


/**
 *
 * @author lgh on 2018/08/29.
 */
@RestController
@RequestMapping("/shop")
@Api(tags = "店铺相关接口")
@AllArgsConstructor
public class ShopDetailController {

    private final ShopDetailService shopDetailService;

	private final UserCollectionShopService userCollectionShopService;

	@GetMapping("/headInfo")
	@ApiOperation(value = "店铺头部信息", notes = "获取的店铺头部信息")
	public ResponseEntity<ShopHeadInfoDto> getShopHeadInfo(Long shopId) {
		ShopHeadInfoDto shopHeadInfoDto = new ShopHeadInfoDto();
		// 粉丝数量
		int fansCount = userCollectionShopService.count(new LambdaQueryWrapper<UserCollectionShop>().eq(UserCollectionShop::getShopId, shopId));
		ShopDetail shopDetail = shopDetailService.getShopDetailByShopId(shopId);
		shopHeadInfoDto.setShopId(shopId);
		shopHeadInfoDto.setShopLogo(shopDetail.getShopLogo());
		shopHeadInfoDto.setShopName(shopDetail.getShopName());
        shopHeadInfoDto.setIntro(shopDetail.getIntro());
		shopHeadInfoDto.setFansCount(fansCount);
		shopHeadInfoDto.setTel(shopDetail.getTel());
		//合并代码开始
		shopHeadInfoDto.setProvince(shopDetail.getProvince());
		shopHeadInfoDto.setProvinceId(shopDetail.getProvinceId());
		shopHeadInfoDto.setCity(shopDetail.getCity());
		shopHeadInfoDto.setCityId(shopDetail.getCityId());
		shopHeadInfoDto.setArea(shopDetail.getArea());
		shopHeadInfoDto.setAreaId(shopDetail.getAreaId());
		//合并代码结束
		return ResponseEntity.ok(shopHeadInfoDto);
	}

	@GetMapping("/hotShops")
	@ApiOperation(value = "热门店铺", notes = "热门店铺")
	public ResponseEntity<List<ShopHeadInfoDto>> hotShops() {
		List<ShopHeadInfoDto> hotShopsHead =  shopDetailService.listHotShopsHead();

		return ResponseEntity.ok(hotShopsHead);
	}



	@GetMapping("/searchShops")
	@ApiOperation(value = "搜索店铺", notes = "根据店铺名称搜索店铺")
	public ResponseEntity<Page<ShopHeadInfoDto>> searchShops(PageParam<ShopHeadInfoDto> page,ShopHeadInfoDto shopHeadInfoDto) {
		Page<ShopHeadInfoDto> hotShopsHeadPage =  shopDetailService.searchShops(page,shopHeadInfoDto);

		return ResponseEntity.ok(hotShopsHeadPage);
	}

	//合并代码开始
	@GetMapping("/updateOrderTypeBasket")
	@ApiOperation(value = "修改加入购物车时的订单类型")
	public ResponseEntity<Void> updateOrderTypeBasket(Integer orderTypeBasket,Long shopId){
		shopDetailService.updateOrderTypeBasket(orderTypeBasket,shopId);
		return ResponseEntity.ok().build();
	}

	@GetMapping("/getAllShop" )
	@ApiOperation(value = "获取所有店铺信息")
	public ResponseEntity<List<ShopDetail>> getAllShop() {
		return ResponseEntity.ok(shopDetailService.getAllShop());
	}

	@GetMapping("getShopDetail")
	@ApiOperation(value = "获取拥有的店铺信息")
	public ResponseEntity<ShopDetail> getShopDetail(String userId) {
		ShopDetail shopDetail = shopDetailService.getShopDetailByUserId(userId);
		return ResponseEntity.ok(shopDetail);
	}
	//合并代码结束

}
